package cn.alpha.open.autocode.generator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;

import cn.alpha.open.autocode.convert.ModelConvertor;
import cn.alpha.open.autocode.model.TemplateModel;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FileGenerator {
	
	private ModelConvertor modelConvertor;
	private Configuration freemarkerCfg;
	private MavenProject project;
	private String resourceDir;
	private String sourceDir;
	private boolean override;
	
	public FileGenerator(ModelConvertor modelConvertor,MavenProject project,boolean override)
	{
		this.modelConvertor=modelConvertor;
		this.project=project;
		this.override=override;
		resourceDir=((Resource)(project.getResources().get(0))).getDirectory();
		sourceDir=project.getBuild().getSourceDirectory();

		freemarkerCfg = new Configuration();
		ClassTemplateLoader classTemplateLoader=new ClassTemplateLoader(FileGenerator.class, "/autocode_tpl");
		freemarkerCfg.setTemplateLoader(classTemplateLoader);
	}
	
	
	private void generateFromModel(List<? extends TemplateModel> models,Template template,String dir) throws UnsupportedEncodingException, FileNotFoundException, TemplateException, IOException
	{
		File file;
		for(TemplateModel model:models)
		{
			file=getFile(dir,model);
			if(file.exists()&&!override)
				continue;
			template.process(model, new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
		}
	}
	
	private File getFile(String dir, TemplateModel model)
	{
		String path=model.getPackageName().replace(".", File.separator);
		String fileDir=dir+File.separator+path;
		
		File newsDir = new File(fileDir);  
        if (!newsDir.exists()) {  
            newsDir.mkdirs();  
        }  
		return new File(fileDir+File.separator+model.getFileName());
	}
	
	public void generate() 
	{
		try {
			Template controllerTpl = freemarkerCfg.getTemplate("controller.ftl");
			Template entityTpl = freemarkerCfg.getTemplate("entity.ftl");
			Template mapperInterfaceTpl = freemarkerCfg.getTemplate("mapperInterface.ftl");
			Template mapperXmlTpl = freemarkerCfg.getTemplate("mapperXml.ftl");
			Template serviceImplTpl = freemarkerCfg.getTemplate("serivceImpl.ftl");
			Template serviceInterfaceTpl = freemarkerCfg.getTemplate("serivceInterface.ftl");
			
			generateFromModel(modelConvertor.getControllerModels(),controllerTpl,sourceDir);
			generateFromModel(modelConvertor.getEntityModels(),entityTpl,sourceDir);
			generateFromModel(modelConvertor.getMappingInterfaceModels(),mapperInterfaceTpl,sourceDir);
			generateFromModel(modelConvertor.getSerivceModels(),serviceImplTpl,sourceDir);
			generateFromModel(modelConvertor.getSerivceInterfaceModels(),serviceInterfaceTpl,sourceDir);
			generateFromModel(modelConvertor.getMappingXmlModels(),mapperXmlTpl,resourceDir);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		} 
	}
	
	
	
	
}
